package nancal.mp.mvc.sys

import io.swagger.annotations.ApiOperation
import nancal.mp.db.mongo.MobileCodeModuleEnum
import nancal.mp.service.sms.SmsStrategy
import nbcp.base.annotation.Require
import nbcp.base.annotation.mvc.OpenAction
import nbcp.base.comm.ApiResult
import nbcp.base.comm.JsonResult
import nbcp.base.extend.AsString
import nbcp.base.extend.HasValue
import nbcp.base.utils.MyUtil
import nbcp.myoql.db.db
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/open")
@OpenAction
class SmsController {

    @Autowired
    lateinit var smsService: SmsStrategy

    @ApiOperation(value = "发送短信通知")
    @PostMapping("/sms-notification/send")
    fun sendSmsNotification(
        productLineCode: String,
        @Require module: MobileCodeModuleEnum,
        @Require mobile: String,
        lang: String = "",
        vararg params: String,
    ): ApiResult<String> {
        return smsService.sendSmsNotification(
            productLineCode,
            module,
            mobile,
            lang,
            params.toList()
        );
    }

    //根据手机号发送验证码功能
    @ApiOperation(value = "发送验证码")
    @PostMapping("/sms-code/send")
    fun sendSmsCode(
        productLineCode: String,
        @Require module: MobileCodeModuleEnum,
        @Require mobile: String,
        code: String,
        lang: String = ""
    ): ApiResult<String> {
        return smsService.sendSmsCode(
            productLineCode,
            module,
            mobile,
            code.AsString(MyUtil.getRandomNumber(1000, 9999).toString()),
            lang
        );
    }


    /**
     * 获取验证码功能,不删code
     */
    @ApiOperation(value = "获取验证码")
    @PostMapping("/sms-code/get")
    fun getSmsCode(
        productLineCode: String,
        @Require module: MobileCodeModuleEnum,
        @Require mobile: String
    ): ApiResult<String> {
        return smsService.getSmsCode(productLineCode, module, mobile);
    }


    /**
     * 验证通过删code
     */
    @PostMapping("/sms-code/checkCode")
    fun check(
        productLineCode: String,
        module: MobileCodeModuleEnum,
        @Require mobile: String,
        @Require code: String
    ): JsonResult {
        val smsCodeResult = getSmsCode(productLineCode, module, mobile);
        if (smsCodeResult.msg.HasValue) {
            return JsonResult.error(smsCodeResult.msg)
        }
        if (smsCodeResult.data != code) {
            return JsonResult.error("验证码错误")
        }


        db.redis.deleteKeys(
            listOf(
                productLineCode,
                module.toString(),
                mobile
            ).filter { it.HasValue }
                .joinToString("-")
        )

        return JsonResult()
    }

    //验证 手机短信验证码 是否正确
//    @PostMapping("/checkCode")
//    fun codeStatus(
//        module: MobileCodeModuleEnum,
//        @Require mobile: String,
//        @Require code: String
//    ): JsonResult {
//        val smsCodeResult = getSmsCode(module, mobile);
//        if (smsCodeResult.msg.HasValue) {
//            return JsonResult.error(smsCodeResult.msg)
//        }
//        if (smsCodeResult.data != code) {
//            return JsonResult.error("验证码错误")
//        }
//
//
//        rer.sys.smsCode.deleteKeys(module.toString() + "-" + mobile)
//        return JsonResult()
//    }
}